package dk.mil.triton.hest;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.*;

public class Hest extends Activity {

    private TextView upperLimitLabel, lowerLimitLabel, feedbackLabel;
    private EditText guessInput;
    private Button newGameButton;
    private TableLayout inputContainer;

    private IntervalView graph;

    private Game game;

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        lowerLimitLabel = (TextView) this.findViewById(R.id.above_field);
        upperLimitLabel = (TextView) this.findViewById(R.id.below_field);
        feedbackLabel = (TextView) this.findViewById(R.id.feedback_label);
        guessInput = (EditText) this.findViewById(R.id.guess_input);
        inputContainer = (TableLayout) findViewById(R.id.input_container);
        newGameButton = (Button) findViewById(R.id.new_game_button);
        graph = (IntervalView) findViewById(R.id.interval_graph);
        if (game == null) {
            game = new Game();
            newGame();
        }
    }

    private void newGame() {
        game.newGame();
        updateLimits();
        feedbackLabel.setTextColor(Color.GREEN);
        feedbackLabel.setText("");
        inputContainer.setVisibility(View.VISIBLE);
        newGameButton.setVisibility(View.GONE);
    }

    private void updateLimits() {
        lowerLimitLabel.setText(""+game.getLower());
        upperLimitLabel.setText(""+game.getUpper());
        graph.updateLimits(game.getLower(),game.getUpper());
    }

    private void showFeedback(GuessResult result) {
        switch (result) {
            case TOO_HIGH:
                feedbackLabel.setText(R.string.too_high);
                break;
            case TOO_LOW:
                feedbackLabel.setText(R.string.too_low);
                break;
            case CORRECT:
                feedbackLabel.setText(R.string.guess_correct);
                feedbackLabel.setTextColor(Color.RED);
                break;
        }
    }



    /**
     * Event handler for the guess button event
     * @param view the event view
     */
    public void guessClicked(View view) {
        Editable text = guessInput.getText();
        String guessString = text.toString();
        if (guessString.length() > 0) {
            int guess = Integer.parseInt(guessString);
            boolean valid = game.validGuess(guess);
            if (valid) {
                GuessResult guessResult = game.guess(guess);
                switch (guessResult) {
                    case TOO_HIGH:
                    case TOO_LOW:
                    updateLimits();
                        break;
                    case CORRECT:
                        graph.updateLimits(guess,guess);
                        //hide input and show new game button!
                        inputContainer.setVisibility(View.GONE);
                        newGameButton.setVisibility(View.VISIBLE);
                        break;
                }
                showFeedback(guessResult);
            } else {
                Toast.makeText(this, R.string.invalid_input,5000);
            }
            text.clear(); //Reset the view whatever happens
        } else {
            return; //Nothing to do on empty input
        }
    }

    public void newGameClicked(View view) {
        newGame();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.new_game_menu_item) {
            newGame();
        }
        return true;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.mainmenu,menu);
        return true;
    }
}
